package com.ht.system.controller;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ht.common.core.convert.EntityDtoConverter;
import com.ht.common.core.domain.R;
import com.ht.common.security.annotation.InnerAuth;
import com.ht.system.api.dto.BaseFieldInfoDto;
import com.ht.system.api.dto.ChannelInfoDto;
import com.ht.system.api.dto.CustomsDto;
import com.ht.system.domain.Customs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ht.common.core.convert.EntitySelectConverter;
import com.ht.common.core.domain.vo.EntitySelectVo;
import com.ht.common.core.web.controller.BaseController;
import com.ht.common.core.web.domain.AjaxResult;
import com.ht.common.core.web.page.TableDataInfo;
import com.ht.common.log.annotation.Log;
import com.ht.common.log.enums.BusinessType;
import com.ht.common.security.annotation.RequiresLogin;
import com.ht.common.security.annotation.RequiresPermissions;
import com.ht.common.security.utils.SecurityUtils;
import com.ht.system.domain.BaseFieldInfo;
import com.ht.system.service.IBaseFieldInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 场所信息Controller
 *
 * @author zmx
 * @date 2025-03-22
 */
 @Api(tags = "场所信息接口")
@RestController
@RequestMapping("/baseFieldInfo")
public class BaseFieldInfoController extends BaseController
{
    @Autowired
    private IBaseFieldInfoService baseFieldInfoService;

    /**
     * 查询场所信息列表
     */
    @ApiOperation("列表,权限:system:baseFieldInfo:list")
    @RequiresPermissions("system:baseFieldInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(BaseFieldInfo baseFieldInfo)
    {
        startPage();
        List<BaseFieldInfo> list = baseFieldInfoService.selectBaseFieldInfoList(baseFieldInfo);
        return getDataTable(list);
    }
    @ApiOperation("下来列表不分页")
    @RequiresLogin
    @GetMapping("/findList")
    public AjaxResult findList(BaseFieldInfo baseFieldInfo)
    {
    	List<BaseFieldInfo> list = baseFieldInfoService.selectBaseFieldInfoList(baseFieldInfo);
    	List<EntitySelectVo> result= EntitySelectConverter.convert(
    			list,
    			BaseFieldInfo::getFieldCode,
    			BaseFieldInfo::getFieldName,
    			BaseFieldInfo::getFieldType
    		);
    	return AjaxResult.success(result);
    }


    private List<EntitySelectVo> toSelectList(List<BaseFieldInfo> list) {
    	return Optional.ofNullable(list)
                .orElseGet(Collections::emptyList)
                .stream()
                .filter(Objects::nonNull) // 过滤掉列表中的null元素
                .map(bf -> new EntitySelectVo(bf.getFieldCode(), bf.getFieldName()))
                .collect(Collectors.toList());
	}
	/**
     * 获取场所信息详细信息
     */
    @ApiOperation("详细信息,权限:system:baseFieldInfo:list")
    @RequiresPermissions("system:baseFieldInfo:list")
    @GetMapping(value = "/getInfo/{fieldCode}")
    public AjaxResult getInfo(@PathVariable("fieldCode") String fieldCode)
    {
        return AjaxResult.success(baseFieldInfoService.selectBaseFieldInfoByFieldCode(fieldCode));
    }

    /**
     * 新增场所信息
     */
    @ApiOperation("新增,权限:system:baseFieldInfo:add")
    @RequiresPermissions("system:baseFieldInfo:add")
    @Log(title = "场所信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody BaseFieldInfo baseFieldInfo)
    {
    			baseFieldInfo.setCreateBy(SecurityUtils.getNickName());
    			baseFieldInfo.setCreateTime(new Date());
        return toAjax(baseFieldInfoService.insertBaseFieldInfo(baseFieldInfo));
    }

    /**
     * 修改场所信息
     */
    @ApiOperation("修改,权限:system:baseFieldInfo:edit")
    @RequiresPermissions("system:baseFieldInfo:edit")
    @Log(title = "场所信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody BaseFieldInfo baseFieldInfo)
    {
    			baseFieldInfo.setUpdateBy(SecurityUtils.getNickName());
    			baseFieldInfo.setUpdateTime(new Date());
        return toAjax(baseFieldInfoService.updateBaseFieldInfo(baseFieldInfo));
    }

    /**
     * 删除场所信息
     */
    @ApiOperation("删除,权限:system:baseFieldInfo:remove")
    @RequiresPermissions("system:baseFieldInfo:remove")
    @Log(title = "场所信息", businessType = BusinessType.DELETE)
	@GetMapping("remove/{fieldCodes}")
    public AjaxResult remove(@PathVariable("fieldCodes") String[] fieldCodes)
    {
        return toAjax(baseFieldInfoService.deleteBaseFieldInfoByFieldCodes(fieldCodes));
    }

    @InnerAuth
    @GetMapping("findByFieldCode/{fieldCode}")
    public R<BaseFieldInfoDto> findByFieldCode(@PathVariable("fieldCode") String fieldCode)
    {
        BaseFieldInfo baseFieldInfo=  baseFieldInfoService.selectBaseFieldInfoByFieldCode(fieldCode);
        return R.ok(EntityDtoConverter.convert(baseFieldInfo, BaseFieldInfoDto.class));
    }

    @InnerAuth
    @GetMapping("findListByCustomsCode/{customsCode}")
    public R<List<BaseFieldInfoDto>> findListByCustomsCode(@PathVariable("customsCode") String customsCode)
    {
        List<BaseFieldInfo> baseFieldInfoList=  baseFieldInfoService.selectByCustomsCode(customsCode);
        return R.ok(EntityDtoConverter.convertList(Collections.singleton(baseFieldInfoList),BaseFieldInfoDto.class));
    }

    @InnerAuth
    @GetMapping("findAllFieldInfo")
    public R<List<BaseFieldInfoDto>> findAllFieldInfo()
    {
        BaseFieldInfo baseFieldInfo = new BaseFieldInfo();
        List<BaseFieldInfo> list = baseFieldInfoService.selectBaseFieldInfoList(baseFieldInfo);
        if(CollectionUtil.isNotEmpty(list)){
            List<BaseFieldInfoDto> baseFieldInfoDtoList = list.stream().map((item) -> {
                BaseFieldInfoDto bean = BeanUtil.toBean(item, BaseFieldInfoDto.class);
                return bean;
            }).collect(Collectors.toList());
            return R.ok(baseFieldInfoDtoList);
        }
        List<BaseFieldInfoDto> dtos = toListDto(list);
        return R.ok(dtos);
    }


    private List<BaseFieldInfoDto> toListDto(List<BaseFieldInfo> baseFieldInfoList) {
        List<BaseFieldInfoDto> listDto=Optional.ofNullable(baseFieldInfoList).orElseGet(Collections::emptyList).stream().map((item)->{
            return toDto(item);
        }).collect(Collectors.toList());
        return listDto;
    }
    private BaseFieldInfoDto toDto(BaseFieldInfo item) {
        BaseFieldInfoDto bd=new BaseFieldInfoDto();
        bd.setGateNum(item.getGateNum());
        bd.setTradeIEFlag(item.getTradeIEFlag());
        bd.setFieldName(item.getFieldName());
        bd.setEntName(item.getEntName());
        bd.setTradeType(item.getTradeType());
        bd.setCustomsCode(item.getCustomsCode());
        bd.setFieldCode(item.getFieldCode());
        bd.setOpenChargeMark(item.getOpenChargeMark());
        bd.setFieldType(item.getFieldType());
        bd.setEntCode(item.getEntCode());
        return bd;
    }

}
